package br.ufpe.abaco.ParserGen;

public class List {
    public Object first;
    public List next;
    public List childs;


    public List(Object f,List n, List c) {
	first = f;
	next = n;
	childs = c;
    }
    public List(Object f,List n) {
	first = f;
	next = n;
	childs = null;
    }

    public List(Object f) {
        first = f;
        next  = null;
	childs = null;
    }


    public List inverse() {
        if (next==null)
            return this;
        return concat(next.inverse(),new List(first,null,childs));
    }

    public int size() {
        if (next==null)
            return 1;
        else 
            return next.size()+1;
    }

    public static List concat(List l1,List l2) {
	if (l1==null)
	    return l2;
	if (l2==null)
	    return l1;
	return new List(l1.first,concat(l1.next,l2),l1.childs);
    }

    public Object[] toArray(Object []type) {

	
        Object dest[];

	if (type.length<size())
	    dest = (Object[])
		java.lang.reflect.Array.
		newInstance(type.getClass().getComponentType(),size());
	else
	    dest = type;
        List corr = this;
        int c=0;
        while (corr!=null) {
            dest[c++] = corr.first;
            corr = corr.next;
        }
        return dest;
    }

    public String toString() {
	String resul = first.toString();
	if (childs!=null)
	    resul += "[ " + childs + " ]";
	if (next!=null)
	    resul += " : " + next.toString();
	return resul;
    }

}
